/**
 * 
 */
package com.yidu.drug.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.model.DrugManagerModel;
import com.yidu.drug.model.FilialeModel;
import com.yidu.drug.model.PolicyModel;
import com.yidu.drug.service.DrugManagerService;
import com.yidu.drug.service.FilialeService;
import com.yidu.drug.service.PolicyService;

/**
 * 政策action
 * @author 吴淑侦
 * @date 2018年8月13日 上午8:57:55
 * @version 1.0
 */
@Controller
@RequestMapping("/policy")
public class PolicyAction {

	@Resource
	private PolicyService policyService;
	@Resource
	private FilialeService filialeService;
	@Resource
	private DrugManagerService drugManagerService;
	
	/**
	 * 显示所有
	 * @param page
	 * @param limit
	 * @param policyName
	 * @param policyComment
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public Map<String, Object> findAll(String page,String limit,String policyName,String policyComment) throws UnsupportedEncodingException{
		Page pages = new Page();
		pages.setPage(Integer.valueOf(page));
		pages.setRows(Integer.valueOf(limit));
		
		PolicyModel policyModel = new PolicyModel();
		policyModel.setPolicyName(policyName);
		policyModel.setPolicyComment(policyComment);
		policyModel.setPage(pages);
		
		List<PolicyModel> list = policyService.findAll(policyModel);
		for (PolicyModel policyModel2 : list) {
			//时间转换
			Date time=policyModel2.getPolicyTime();
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String tsStr = sdf.format(time.getTime());
	        policyModel2.setTime(tsStr);
		}
		Long count = policyService.findRows(policyModel);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", list);
		return map;	
	}
	
	/**
	 * 删除(修改状态)
	 * @param filId
	 * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	public String updateById(String policyId){
		return policyService.updateById(policyId);
	
		
	}
	
	/**
	 * 增加
	 * @param policyModel
	 * @param session
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/add")
	public void add(PolicyModel policyModel,HttpSession session) throws UnsupportedEncodingException{
		String policyName = new String(policyModel.getPolicyName().getBytes("ISO8859-1"),"UTF-8");
		String policyComment = new String(policyModel.getPolicyComment().getBytes("ISO8859-1"),"UTF-8");
		String policyRemark = new String(policyModel.getPolicyRemark().getBytes("ISO8859-1"),"UTF-8");
		
		String policyId = UUID.randomUUID().toString();
		Timestamp policyTime = new Timestamp(new Date().getTime());
		String policyPerson = "1001";
		//session.getAttribute("manager");
		String filName = "";
		String companyName = "";
		
		DrugManagerModel drugManagerModel = drugManagerService.findByUsId(policyPerson);
		
		policyModel.setPolicyId(policyId);
		policyModel.setPolicyName(policyName);
		policyModel.setPolicyComment(policyComment);
		policyModel.setPolicyRemark(policyRemark);
		policyModel.setPolicyTime(policyTime);
		policyModel.setIsva("有效");
		policyModel.setPolicyPerson(policyPerson);
		policyModel.setPolicyCompany(drugManagerModel.getFilId());
		
		System.out.println(drugManagerModel.getManUser()+"============"+drugManagerModel.getFilId());
		
		String[] str = policyModel.getFilIds().split(",");
		for(int i=0;i<str.length;i++){
			if(!Tools.isEmpty(str[i])){
				FilialeModel filialeModel = filialeService.findById(str[i]);
				filName = filialeModel.getFilName();
				if(companyName==""){
					companyName = filName;
				}else{
					companyName = companyName+","+filName;
				}
			}
		}
		policyModel.setCompanyName(companyName);
		
		policyService.save(policyModel);
		
	}
	
	/**
	 * 显示修改页面
	 * @param request
	 * @param response
	 * @param session
	 * @param policyId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public PolicyModel showUpdate(HttpServletRequest request,HttpServletResponse response,HttpSession session,String policyId) throws IOException{
		PolicyModel policyModel = policyService.findById(policyId);
		
		return policyModel;
	}
	
	/**
	 * 修改的方法
	 * @param policyModel
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/update")
	public void update(PolicyModel policyModel) throws UnsupportedEncodingException{
		String policyName = new String(policyModel.getPolicyName().getBytes("ISO8859-1"),"UTF-8");
		String policyComment = new String(policyModel.getPolicyComment().getBytes("ISO8859-1"),"UTF-8");
		String policyRemark = new String(policyModel.getPolicyRemark().getBytes("ISO8859-1"),"UTF-8");
		
		Timestamp policyTime = new Timestamp(new Date().getTime());
		String filName = "";
		String companyName = "";
		
		policyModel.setPolicyName(policyName);
		policyModel.setPolicyComment(policyComment);
		policyModel.setPolicyRemark(policyRemark);
		policyModel.setPolicyTime(policyTime);
		
		String[] str = policyModel.getFilIds().split(",");
		for(int i=0;i<str.length;i++){
			if(!Tools.isEmpty(str[i])){
				FilialeModel filialeModel = filialeService.findById(str[i]);
				filName = filialeModel.getFilName();
				if(companyName==""){
					companyName = filName;
				}else{
					companyName = companyName+","+filName;
				}
			}
		}
		policyModel.setCompanyName(companyName);
		policyService.update(policyModel);
	}
	
	/**
	 * 查询
	 * @param policyModel
	 * @param policyId
	 * @return
	 */
	@RequestMapping("/findByPolicyId")
	@ResponseBody
	public Map<String,Object> findByPolicyId(HttpSession session, PolicyModel policyModel,String filIds){
		
		List<PolicyModel> list= policyService.findByPolicyId(policyModel);
		
		for (PolicyModel policyModel2 : list) {
			Date time=policyModel2.getPolicyTime();
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String tsStr = sdf.format(time.getTime());
	        policyModel2.setTime(tsStr);
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("list", list);
		
		return map;
		
	}
	
}
